package com.daz.test.springwebservice.config;

import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.io.ClassPathResource;
import org.springframework.ws.wsdl.wsdl11.DefaultWsdl11Definition;
import org.springframework.xml.xsd.SimpleXsdSchema;
import org.xml.sax.SAXException;

import javax.xml.parsers.ParserConfigurationException;
import java.io.IOException;

@Configuration
@ConfigurationProperties(prefix = "app.webservice")
public class WebServiceConfig {

    private String serviceUrl;

    public String getServiceUrl() {
        return serviceUrl;
    }

    public void setServiceUrl(String serviceUrl) {
        this.serviceUrl = serviceUrl;
    }

    @Bean
    public DefaultWsdl11Definition example() throws IOException, SAXException, ParserConfigurationException {
        DefaultWsdl11Definition definition = new DefaultWsdl11Definition();
        definition.setPortTypeName("example");
        definition.setLocationUri(serviceUrl + "/services/example");
        SimpleXsdSchema schema = new SimpleXsdSchema(new ClassPathResource("schema/example.xsd"));
        schema.afterPropertiesSet();
        definition.setSchema(schema);
        return definition;
    }

}
